import { getCrawlList, getCrawlAllList, deleteCrawl } from '../../api/crawl.js'
import { getLineList } from '../../api/common.js'
Page({
  data: {
    slideButtons: [{
      type: 'warn',
      text: '删除',
      extClass: 'test',
    }],
    tableList: [],
    height: '',
    showType: '',
    lineIndex: 0,
    lineList: [],
    loadMoreNum: 10,
  },
  onLoad: function () {
    let that = this;
    wx.getSystemInfo({
      success: function (res) {
        let clientHeight = res.windowHeight;
        let clientWidth = res.windowWidth;
        let ratio = 750 / clientWidth;
        // let height = clientHeight - 50;
        let height = res.windowHeight;
        console.log('height', height)
        that.setData({
          height: height
        });
      }
    })
    this.getTableList(getCrawlAllList({lineId: null}));
    this.getList();
  },
  // 查询
  // 输入框输入
  formInputChange(e){
    console.log('输入', e)
  },
  // 输入框回车
  searchConfirm(e){
    console.log('回车',e)
  },
  // 滚动加载更多
  lower() {
    console.log('加载')
    var result = this.data.tableList;
    var id = this.data.tableList[this.data.tableList.length - 1].id;
    var lineId = this.data.lineIndex != 0 ? this.data.lineList[this.data.lineIndex].id : null;
    var resArr = [];
    this.setData({
      showType: 1,
    });
    getCrawlList(id, {lineId: lineId}).then(res=>{
      resArr = res.data;
      var cont = result.concat(resArr);
      this.setData({
        tableList: cont,
        showType: '',
      });
      if (resArr.length == 10){
        this.setData({
          showType: '',
        });
      }else{
        this.setData({
          showType: 2,
        });
      }
    }).catch(err=>{

    })
  },
  // 新增

  // 编辑
  editCrawl(e){
    console.log('e',e)
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/crawlEdit/crawlEdit?type=' + 'edit' + '&id=' + id,
    })
  },
  // 删除
  slideButtonTap(e) {
    var id = e.currentTarget.dataset.id;
    var lineId = this.data.lineIndex != 0 ? this.data.lineList[this.data.lineIndex].id : null;
    deleteCrawl([id]).then(res=>{
      wx.showToast({
        title: res.msg,
        icon: 'success',
      })
      this.getTableList(getCrawlAllList({ lineId: lineId }));
    }).catch(err=>{

    })
  },
  // 选择产品线
  bindlineChange(e) {
    console.log('picker account 发生选择改变，携带值为', e.detail.value);
    this.setData({
      lineIndex: e.detail.value
    })
    var lineId = this.data.lineIndex!=0 ? this.data.lineList[this.data.lineIndex].id : null;
    this.getTableList(getCrawlAllList({ lineId: lineId }));
  },
  getTableList(func){
    func.then(res => {
      this.setData({
        tableList: res.data,
      });
    }).catch(err => {

    })
  },
  getList(){
    getLineList('').then(res=>{
      var arr = [{ lineName: '全部'}];
      arr = arr.concat(res.data);
      this.setData({
        lineList: arr
      });
    }).catch(err=>{

    })
  },
});